package com.neteast.androidclient.contact.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.neteast.androidclient.contact.R;
import com.neteast.androidclient.contact.activity.ContactActivity;
import com.neteast.androidclient.contact.domain.Contact;
import com.neteast.androidclient.contact.domain.ContactData;
import com.neteast.androidclient.contact.util.ImageUtil;
import com.neteast.androidclient.contact.util.NetUtil;

public class SingleScreenAdapter extends BaseAdapter {

	private Context context;
	private List<Contact> data;
	private Contact contact;
	private int page;
	private Handler handler;
	private int currentView;
	private String avatarStr;
	private String name;
	private String company;
	private String job;
	private String phone;
	private String email;
	private Thread deleteThread;
	private Dialog pDialog;
	private ImageUtil imageUtil;
	

	// 每页显示的Item个数
	public static final int SIZE = 12;

	public SingleScreenAdapter(Context mContext, List<Contact> list, 
			int page, Handler handler, int currentView, Dialog pDialog) {
		this.context = mContext;
		this.page = page;
		this.handler = handler;
		this.currentView = currentView;
		this.pDialog = pDialog;
		data = new ArrayList<Contact>();
		int i = page * SIZE;
		int iEnd = i + SIZE;
		while ((i < list.size()) && (i < iEnd)) {
			data.add(list.get(i));
			i++;
		}
		imageUtil = ImageUtil.getInstance(mContext);
	}

	public int getCurrentPage(){
		return page;
	}
	
	public int getCount() {
		return data.size();
	}

	public Object getItem(int position) {
		return data.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public void addItem(Contact contact){
		data.add(contact);
		notifyDataSetChanged();
	}
	
	public void modifyItem(int position, Contact contact){
		data.set(position, contact);
		notifyDataSetChanged();
	}
	
	public void enterManager(){
		for(Contact contact : data){
			contact.isDelete = true;
		}
		notifyDataSetChanged();
	}
	
	public void exitManager(){
		for(Contact contact : data){
			contact.isDelete = false;
		}
		notifyDataSetChanged();
	}
	
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (data == null) {
			return null;
		}
		ViewHolder holder = new ViewHolder();
		contact = data.get(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.contact_card_item, parent,false);
			holder = new ViewHolder();
			holder.avatarImg = (ImageView) convertView.findViewById(R.id.imageView);
			holder.nameText = (TextView) convertView.findViewById(R.id.name);
			holder.companyText = (TextView) convertView.findViewById(R.id.company);
			holder.positionText = (TextView) convertView.findViewById(R.id.position);
			holder.phoneText = (TextView) convertView.findViewById(R.id.phone);
			holder.emailText = (TextView) convertView.findViewById(R.id.email);
			holder.deleteIcon = (ImageView) convertView.findViewById(R.id.iconDelete);
			holder.cloudIcon = (ImageView) convertView.findViewById(R.id.isCloud);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		avatarStr = contact.photo;
		name = contact.displayName;
		company = contact.organization == null ? "" : contact.organization;
		job = contact.job == null ? "" : contact.job;
		phone = contact.fphone == null ? "" : contact.fphone;
		email = contact.femail == null ? "" : contact.femail;
		holder.avatarImg.setImageResource(R.drawable.default_photo);
		imageUtil.setImageView(handler, contact, holder.avatarImg);
		Log.d("SingleScreenAdapter", "-------------------setImage");
		holder.nameText.setText(data.get(position).displayName);
		holder.companyText.setText(data.get(position).organization);
		holder.positionText.setText(data.get(position).job);
		holder.phoneText.setText(data.get(position).fphone);
		holder.emailText.setText(data.get(position).femail);
		if(contact.isReadOnly){
			holder.cloudIcon.setVisibility(View.VISIBLE);
		}else{
			holder.cloudIcon.setVisibility(View.GONE);
		}
		if(contact.isDelete){
			holder.deleteIcon.setVisibility(View.VISIBLE);
		}else{
			holder.deleteIcon.setVisibility(View.GONE);
		}
		holder.deleteIcon.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				final View dialogView = LayoutInflater.from(context).inflate(
						R.layout.dlg_add_group, null);
				final EditText messageEdit = ((EditText) dialogView
						.findViewById(R.id.messageEdit));
				final TextView messageText = ((TextView) dialogView
						.findViewById(R.id.messageText));
				final TextView titleText = ((TextView) dialogView
						.findViewById(R.id.titleText));
				messageEdit.setVisibility(View.GONE);
				messageText.setText("删除该联系人?");
				titleText.setText(context.getResources().getString(R.string.delete_group_title));
				final Dialog deleteDialog = new Dialog(context,
						R.style.DialogStyle);
				deleteDialog.setContentView(dialogView);
				deleteDialog.show();
				WindowManager.LayoutParams params = deleteDialog.getWindow()
						.getAttributes();
				params.width = ContactActivity.DLG_DELETE_GROUP_WIDTH;
				params.height = ContactActivity.DLG_DELETE_GROUP_HEIGHT;
				deleteDialog.getWindow().setAttributes(params);
				((Button) dialogView.findViewById(R.id.btnOk))
						.setOnClickListener(new OnClickListener() {

							public void onClick(View v) {
								new ContactActivity().showProgressDialog(context, pDialog, "正在删除好友,请稍候");
								if(currentView == 1){
									deleteThread = new Thread(new Runnable() {
										
										public void run() {
											try {
												NetUtil.deleteContact(context, data.get(position));
												Message msg = new Message();
												msg.what = ContactActivity.DELETE_CLOUD_ITEM;
												msg.arg1 = position;
												msg.arg2 = page;
												handler.sendMessage(msg);
											} catch (Exception e) {
												handler.obtainMessage(ContactActivity.ERROR, 0, 0, "删除失败");
												e.printStackTrace();
											}
											
											
										}
									});
									
								} else if (currentView == 2) {
									deleteThread = new Thread(new Runnable() {

										public void run() {
											try {
												Log.d("DataAdapter", data.get(position).contactId+"-------------------------");
												NetUtil.deleteContact(context, data.get(position));
												Message msg = new Message();
												msg.what = ContactActivity.DELETE_COMM_ITEM;
												msg.arg2 = page;
												msg.arg1 = position;
												handler.sendMessage(msg);
											} catch (Exception e) {
												handler.obtainMessage(
														ContactActivity.ERROR,
														0, 0, "删除失败");
												e.printStackTrace();
											}

										}
									});

								}
								deleteThread.start();
								deleteDialog.cancel();

							}
						});
				((Button) dialogView.findViewById(R.id.btnCancel))
						.setOnClickListener(new OnClickListener() {

							public void onClick(View v) {
								deleteDialog.cancel();

							}
						});
			}
		});
		return convertView;
	}

	class ViewHolder{
		ImageView avatarImg;
		TextView nameText;
		TextView companyText;
		TextView positionText;
		TextView phoneText;
		TextView emailText;
		ImageView deleteIcon;
		ImageView cloudIcon;
	}
}
